
function maskDialog() {
    this.hide = function() {
        var el = $("div_Mask");
        if(el) {
            this.delayHide(el);
        }
    };
    this.delayHide = function(el) {
        el.setStyle('display', 'none');
    };
    this.show = function() {
    	if(!document.getElementById('div_Mask')){
	        document.writeln('<div id="div_Mask" style="width:100%;position:absolute; z-index:99999; top:0; left:0; background:white; height:'+0/*document.documentElement.clientHeight*/+'px;"><div style="z-index:99999;font-size:12px;color:#FFFFFF;background:#FF0000;position:absolute;top:0px;right:0px;padding:2px 10px 2px 10px;">' +
	                '加载中，请稍后..........</div></div>');
	        document.close();
	    }
		return this;
    };
}
var MaskDialog = new maskDialog().show();

/*
window.onerror=function(s){
    if(pc&&pc.getMask()){
         pc.getMask().unmask();
    }
};
*/

$l=(function(){
	function isLog() {
	    var location = "" + window.location;
	    return (location.indexOf("log=true") != -1);
	}
	
	function swordlog(){
	    var t={};
	    var d=[];
	    this.log=function(n){
	        if(t[n]){
	           var tmp=  (new Date().getTime()-t[n]);
	           d.push(n+"="+tmp);
	           delete t[n];
	        }else{
	           t[n]=new Date().getTime();
	        }
	    };
	    this.flush=function(){
	    	var sla=$('SwordLogArea');
	    	if(!sla){
	    		sla=new Element('div',{
	    			'id':'SwordLogArea'
	    		}).inject(document.body,'top');
	    	}
	    	sla.innerHTML+=d.join("  <br> ");
	        d.empty();
	    }
	}
	if(isLog())return new swordlog();
	else return {log:function(){},flush:function(){}};
	
})();

$l.log('init');

$SwordLoader=(function(){
    function isServer(){
        if(document.getElementsByTagName("script")[0]['edit'] == 'true') {
            return false;
        } else {
            return !document.domain == "";
        }
    }

    function isIE(){
          return (navigator.userAgent.toLowerCase().indexOf('msie') != -1);
    }

    function writeLocalJs(type, source) {
        var oHead = document.getElementsByTagName('HEAD').item(0);
        var oScript = document.createElement("script");
        oScript.language = "javascript";
        oScript.type = "text/javascript";
        oScript.defer = true;
        oScript[type] = source;
        oHead.appendChild(oScript);
        return oScript;
    }

    function importJs(path){
        if (this.server) {
            this.writeServerJs(path);
        } else {
            this.writeLocalJs("src", path);
        }
    }

    function writeServerJs(path) {
        var xReq = null;
        if (window.XMLHttpRequest) {
            xReq = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            xReq = new ActiveXObject("MsXml2.XmlHttp");
        }
        xReq.open('get', path, false);
        xReq.setRequestHeader("Content-Type", "text/plain;charset=gb2312");
        xReq.send(null);
        if (xReq.readyState == 4) {
            if (xReq.status == 200) {
                if (this.isIE) {
                	if(xReq.responseText=="") return;
                    execScript(xReq.responseText);
                } else {
                    window.eval(xReq.responseText);
                }
            }
        }
    }

    var swordJS;
    function ChargeAndGetRootPath() {
    	//http://localhost:8081/web/sword?tid=GridBLH_init4
        //swordweb/core/jsimport/Sword.js?runmode=product
        var scripts = document.getElementsByTagName("script");

        var src,srcStr;
        var arraylength=scripts.length;
        for(var tempi=0;tempi<arraylength;tempi++){
               srcStr=scripts[tempi].src;
              if(srcStr.indexOf("swordweb/core/jsimport/Sword")!=-1){
                  src=srcStr;
                  swordJS=scripts[tempi];
                  break;
              }
        }
        if (src) {
            _SWORD_RUNMODE_ = src.substr(src.lastIndexOf('=') + 1);
            return src.substr(0, src.indexOf('swordweb/core/jsimport'));
        } else {
            alert('没有找到sword.js节点');
            return ;
        }
    }

    function writeCSS(path){
        var oHead = document.getElementsByTagName('HEAD').item(0);
        var link = document.createElement("link");
        link.href = path;
        link.type = "text/css";
        link.rel = "stylesheet";
        oHead.appendChild(link);
    }

    return{
        server: isServer()
        ,writeLocalJs: writeLocalJs
        ,writeServerJs:writeServerJs
        ,importJs:  importJs
        ,writeCSS:writeCSS
        ,isIE:isIE()
        ,rootPath:ChargeAndGetRootPath()
        ,getSwordJS:function(){
        	return swordJS;
        }
    };

})();




if(typeof(swordCfg)!="object"){ //开发模式
   $SwordLoader.importJs($SwordLoader.rootPath + "swordweb/core/jsimport/JsReady-dev.js");
   $SwordLoader.importJs($SwordLoader.rootPath + "swordweb/config.js");
} 
/*todo 此处代码由swordwebfilter写入了。
 * else if (_SWORD_RUNMODE_ != "pro"){ //生产模式
    $SwordLoader.writeCSS($SwordLoader.rootPath +"swordweb/styles/"+swordCfg.style.sys_style +"/SwordCss/sword_min.css");
}*/

